import { AnyAction, Reducer } from 'redux';
import { message } from 'antd';
import { EffectsCommandMap } from 'dva';
import { fetchValidation } from '@/utils';
import {
  queryTookManagementList,
  queryTookManagementSendCount,
  queryTookManagementSendTypes,
  queryCancelList,
  operactionCancelOrder,
  queryInforExcal,
  queryCainiaoShippAddr,
  queryPrintConfInfo,
  queryTemplateList,
  setPrintTemplate,
  yunPrint,
} from './service';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetch: Effect;
    sendingCount: Effect;
    sendingTypes: Effect;
    fetchCancelList: Effect;
    cancelOrder: Effect;
    inforExcal: Effect;
    cainiaoShippAddr: Effect;
    printConfInfo: Effect;
    printTemplate: Effect;
    setPrintTemplate: Effect;
    yunPrint: Effect;
  };
  reducers: {
    save: Reducer;
  };
}

const Model: ModelType = {
  namespace: 'tookManagementList',

  state: {
    dataInfo: {
      count: 0,
      list: [],
    },
    cancelList: [],
    typesList: {},
    countInfo: {},
    cainiaoShippAddr: [],
    printConfInfo: {},
    templateList: [],
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(queryTookManagementList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          type: 'dataInfo',
          data: data.body,
        },
      });
    },

    * sendingCount({ payload }, { call, put }) {
      const response = yield call(queryTookManagementSendCount, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          type: 'countInfo',
          data: data.body,
        },
      });
    },

    * sendingTypes({ payload }, { call, put }) {
      const response = yield call(queryTookManagementSendTypes, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          type: 'typesList',
          data: data.body,
        },
      });
    },

    * fetchCancelList({ payload }, { call, put }) {
      const response = yield call(queryCancelList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          type: 'cancelList',
          data: data.body ? data.body.list : [],
        },
      });
    },

    * cancelOrder({ payload: { search, ...query } }, { call, put }) {
      const response = yield call(operactionCancelOrder, query);
      const data = fetchValidation(response);
      if (data) {
        message.success('操作成功');
        yield put({
          type: 'fetch',
          payload: search,
        });
      }
    },

    * inforExcal({ payload }, { call }) {
      yield call(queryInforExcal, payload);
    },

    * cainiaoShippAddr({ payload }, { call, put }) {
      const response = yield call(queryCainiaoShippAddr, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          type: 'cainiaoShippAddr',
          data: data.body,
        },
      });
    },

    * printConfInfo({ payload }, { call, put }) {
      const response = yield call(queryPrintConfInfo, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          type: 'printConfInfo',
          data: data.body,
        },
      });
    },

    * printTemplate({ payload }, { call, put }) {
      const response = yield call(queryTemplateList, payload);
      const data = fetchValidation(response);
      yield data && put({
        type: 'save',
        payload: {
          type: 'templateList',
          data: data.body,
        },
      });
    },

    * setPrintTemplate({ payload }, { call }) {
      const response = yield call(setPrintTemplate, payload);
      fetchValidation(response);
    },

    * yunPrint({ payload }, { call }) {
      return yield call(yunPrint, payload);
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        [payload.type]: payload.data,
      };
    },
  },
};

export default Model;
